package com.huazhi.wzsnw.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DBBackup {
    public static Map<String,String> backup(String path) {
        try {
            Map<String,String> result = new HashMap<String,String>();
            SimpleDateFormat a = new SimpleDateFormat("yyyyMMddHHmm");
            Date d = new Date();
//            int index = path.lastIndexOf("\\");
//            if(index!=-1){
//                path = path.substring(0,index);
//            }
            File file=new File(path + "/backup/");
            if(!file.exists()){//如果文件夹不存在
                file.mkdir();//创建文件夹
            }
            String path1 = path + "/backup/" + a.format(d) + ".sql";
            String path2 = path + "/backup/"+a.format(d)+"/" ;


            Runtime runtime = Runtime.getRuntime();
            // -u后面是用户名，-p是密码-p后面最好不要有空格，-family是数据库的名字
            //Process process = runtime.exec("mysqldump -u root -p1q2w3e sports");
            Process process = runtime.exec(" cmd /c  C:\\mysqldump " + " -h "+PropertyUtil.getText("db.ip")+" -u root -p"+
                            PropertyUtil.getText("jdbc.password")+" --opt wzsnw > "+path1);

//            Process process2 = runtime.exec(" cmd /c  mongodump " + " -h   "+PropertyUtil.getText("mongo.ip")+"   -d cis  -o "+path2);

            result.put("name",a.format(d));
            result.put("url",path1);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //
    public static int load(String Path,String fileName) {
        try {
            String fPath = Path+"/"+fileName+".sql";
            Runtime rt = Runtime.getRuntime();
            String cmd="cmd /c  C:/mysql"+" -h"+PropertyUtil.getText("db.ip")+" -u root -p"+PropertyUtil.getText("jdbc.password")+
                    " wzsnw " +" < "+fPath;
            System.out.println(cmd);
            Process child = rt.exec( cmd);
//            String cmd2="cmd /c  mongorestore "+" -h "+PropertyUtil.getText("mongo.ip")+
//                    " -d cis  --drop " +Path+"/"+fileName+"/cis";

//            System.out.println(cmd2);
//            Process child2 = rt.exec( cmd2);

            return 1;
        } catch (Exception e) {

            e.printStackTrace();
            return 0;
        }

    }

}
